<?php
// 登录注册中心
class User extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    function reg()
    {
        if (! mb_substr($this->input->post('name'), 3, 1, 'utf-8')) {
            $this->_json_back('机构名称最少为4个字.');
        }
        if (! $this->input->post('industry')) {
            $this->_json_back('请选择行业.');
        }
        if (! $this->input->post('province')) {
            $this->_json_back('请选择省份');
        }
        if (! $this->input->post('city')) {
            $this->_json_back('请选择城市');
        }
        if (! $this->input->post('contact')) {
            $this->_json_back('请输入您的称呼.');
        }
        $time_limit_cond = array(
            'mobile' => $this->input->post('mobile'),
            'verify_code' => $this->input->post('verify_code'),
            'create_time >' => time() - 60
        );
        
        $time_limit_result = $this->common_model->getone_where($time_limit_cond, 'verify_codes');
        if (! $time_limit_result) {
            $this->_json_back('验证码已经过期或短信验证码错误.');
        }
        
        if (! substr($this->input->post('password'), 5, 1)) {
            $this->_json_back('密码至少6位');
        }
        
        if (! preg_match("/^1[34578]{1}\d{9}$/", $this->input->post('mobile'))) {
            $this->_json_back('手机号码格式不正确.');
        }
        
        $user = $this->users_model->get_one_by_mobile($this->input->post('mobile'));
        if ($user)
            $this->_json_back('该手机已经注册.');
            
            // 新增用户信息
        $user_info = array(
            'name' => '',
            'mobile' => $this->input->post('mobile'),
            'password' => password_hash(trim($this->input->post('password')), PASSWORD_DEFAULT),
            'organization' => $this->input->post('name'),
            'industry' => $this->input->post('industry'),
            'province' => $this->input->post('province'),
            'city' => $this->input->post('city'),
            'contact' => $this->input->post('contact'),
            'dist' => $this->input->post('dist') ? $this->input->post('dist') : '',
            'status' => 1,
            'open_id' => 0,
            'create_time' => time()
        );
        
        $user_result = $this->users_model->add($user_info);
        if (! $user_result)
            $this->_json_back('注册出现错误，请重试或向客服咨询');
            
            // 返回结果
        $this->_json_back('你已注册成功.', 1);
    }

    /**
     * 获取验证码
     * 
     * @return [type] [description]
     */
    public function verify_code($mobile = '')
    {
        if (! preg_match("/^1[34578]{1}\d{9}$/", $mobile)) {
            $this->_json_back('手机号码格式不正确.');
        }
        
        // 获取用户ID
        $user = $this->users_model->get_one_by_mobile($mobile);
        
        // 获取注册验证码时
        if ($user) {
            $this->_json_back('绑定的手机号码用户已经存在.');
        }
        
        // 限制同一用户获取短信时间
        $time_limit_cond = array(
            'mobile' => $mobile,
            'create_time >' => time() - 60
        );
        $time_limit_result = $this->common_model->getone_where($time_limit_cond, 'verify_codes');
        if ($time_limit_result)
            $this->_json_back('您的操作过于频繁,请稍等再试.');
            
            // 限制同一IP请求次数
        $ip_limit_cond = array(
            'ip' => $this->input->ip_address(),
            'create_time >' => strtotime('today')
        );
        
        $ip_count = $this->common_model->count_where($ip_limit_cond, 'verify_codes');
        if ($ip_count > $this->config->item('ip_limit_count'))
            $this->_json_back('非法IP地址');
            
            // 限制同一手机号码请求次数
        $mobile_limit_cond = array(
            'mobile' => $mobile,
            'create_time >' => strtotime('today')
        );
        $mobile_count = $this->common_model->count_where($mobile_limit_cond, 'verify_codes');
        if ($mobile_count > $this->config->item('sms_limit_count'))
            $this->_json_back('单个手机号码短信每日达到上限');
            
            // 生成验证码
        $verify_code = mt_rand(1000, 9999);
        $this->load->library('sms');
        
        $send_result = $this->sms->sendMsg($mobile, $verify_code); // type:1，注册
                                                                   
        // 发送失败记录日志
        if ($send_result->code) {
            $this->log_re($mobile . ':验证码发送失败.');
            $this->_json_back('短信验证系统发送短信出现错误');
        }
        
        // 记录用户获取验证码信息
        $log_data = array(
            'mobile' => $mobile,
            'ip' => $this->input->ip_address(),
            'verify_code' => $verify_code,
            'create_time' => time()
        );
        $log_result = $this->common_model->add($log_data, 'verify_codes');
        if (! $log_result)
            $this->_json_back('系统错误');
            
            // 暂时返回验证码
        $this->_json_back('验证短信已经发送', 1);
    }
    
    // 商家登录
    public function dologin()
    {
        // 获取用户信息
        $user_cond = array(
            'mobile' => $this->input->post('mobile')
        );
        
        $user = $this->common_model->getone_where($user_cond, 'users', 'id,mobile,organization,password,expire,status');
        
        if (! $user)
            $this->_json_back('用户不存在');
        if ($user['status'] != 1)
            $this->_json_back('暂时无法进行登录，如需帮助，请咨询我们客服。');
            
            // 校验密码
        if (password_verify(trim($this->input->post('password')), $user['password'])) {
            unset($user['password']);
        } else {
            $this->_json_back('登录密码错误。');
        }
        
        $this->common_model->update_byid($user['id'], array(
            'lastlogindate' => date('Y-m-d H:i:s')
        ), 'users');
        $this->_json_back($user['organization'], 1);
    }
    
    // 统一json返回
    final function _json_back($msg = '', $status = 0)
    {
        $json = array(
            'status' => $status,
            'msg' => $msg
        );
        // 手动结束
        if ($this->db) {
            $this->db->close();
        }
        
        header("Content-Type:application/json;charset=utf-8");
        die(json_encode($json));
    }
}

?>